<?php
class SourceControlsController extends AppController {
	public $name = 'SourceControls';
	public $uses=array('SourceControl','Project');
	
	
	
	
	function admin_index($id=0){
		//get filter field		
		$id=intval($id);
		if($id==0)$id=intval($this->Session->read('SourceControl.project_id'));
		$projectInfo=$this->getPermission(0,$id);
		if(empty($projectInfo)){
			$this->Session->write('error',_resource('you have not permission',true));
			$this->redirectAjaxTo('projects','mainContent');
		}
		$this->Session->write('SourceControl.project_id',$id);
		$filterField=array(
		$this->uses[0].'.name'=>_resource('name',true),
		$this->uses[0].'.username'=>_resource('username',true),
		$this->uses[0].'.url'=>_resource('url',true)
		);
		$this->set('project',$projectInfo['Project']);
		$this->set('filterField',$filterField);
		$this->render('admin_index','ajax');
	}

	function admin_lists(){
		//get project id
		$pid=intval($this->Session->read('SourceControl.project_id'));
		$projectInfo=$this->getPermission(0,$pid);
		if(empty($projectInfo)){
			$this->Session->write('error',_resource('you have not permission',true));
			$this->redirectAjaxTo('projects','mainContent');
		}
		//khoi tao cac session can thiet cho 1 model, session nay dung de luu lai trang thai cua 1 model nhu phan trang, sort tang giam, dieu kien search, id cha...
		//cac su dung Session cua cakephp : doc them trong book.cakephp.org
		if(!$this->Session->check($this->uses[0])){
			//khoi tao array vao bien $tmp
			$tmp=array(
				'limit'=>$this->webConfig['default_row_per_page'],
				'conditions'=>'',
				'fields'=>null,
				'order'=>$this->uses[0].'.created desc',
				'page'=>1,
				'recursive'=>1,
				'sortField'=>null,
				'sortDir'=>'asc',				
				'filterField'=>'',
				'filterText'=>'',
				'catid'=>'',
				'status_id'=>$this->Session->read($this->uses[0].'.status_id')
			);
			//luu vao session
			$this->Session->write($this->uses[0],$tmp);
		}

		$user=$this->user;

		//doc ra cac trang thai cua 1 model da luu trong session
		// truyen vao bien paginate, khi lay data, model se dua vao cac gia tri cua bien nay de search, sort, phan trang ...
		$paginate=$this->Session->read($this->uses[0]);
		
		$paginate['conditions']['SourceControl.project_id']=$pid;
		
		
		//ham paginate() la ham ho tro phan trang, khi dung ham nay, se tu dong phan trang theo cac gia tri trong bien paginate o tren
		//lay data da duoc phan trang va truyen vao bien $data0
		$this->paginate=$paginate
		;
		$data0=$this->paginate($this->uses[0]);
		
		//set bien $data0 ra view de hien ra luoi.
		$this->set('data0',$data0);

		//render view va cho cakephp biet day la view cua ajax -> khi do, cakephp se chi render view ma khong render toan bo layout cua website.
		$this->render('admin_lists','ajax');
	}

	function admin_add($locale=null){
		$pid=intval($this->Session->read('SourceControl.project_id'));
		$projectInfo=$this->getPermission(0,$pid);
		if(empty($projectInfo)){
			$this->Session->write('error',_resource('you have not permission',true));
			$this->redirectAjaxTo('projects','mainContent');
		}
		
		$isInsert=false;
		
		if(!empty($this->data)){
			$data=$this->data;
			$data[$this->uses[0]]['user_id']=$this->user['id'];
			$data[$this->uses[0]]['project_id']=$pid;
			//t($data);
			if($this->{$this->uses[0]}->create($data) && $this->{$this->uses[0]}->validates()){
				
				if($this->{$this->uses[0]}->save($data)) {					
					$this->Session->write('message',_resource('insert data successful',true));
					$this->redirectAjaxTo($this->name.'/lists','listContent');					
					$this->data=null;					
					$isInsert=true;
					
				} else {
					$this->Session->write('error',_resource('can not insert data',true));
				}
			}else{
				$this->Session->write('error',_resource('red fill is require',true));
			}
		}

		
		$this->set('isInsert',$isInsert);
	}

	function admin_edit($id=null){
		$pid=intval($this->Session->read('SourceControl.project_id'));
		//t($pid);
		$projectInfo=$this->getPermission(0,$pid);
		if(empty($projectInfo)){
			$this->Session->write('error',_resource('you have not permission',true));
			$this->redirectAjaxTo('projects','mainContent');
		}
		

		$isInsert=false;
		
		if(!empty($this->data)){
			$data=$this->data;			
			//check sc is belong to project
			$sc=$this->{$this->uses[0]}->read(null,intval($data[$this->uses[0]]['id']));
			
			if(empty($sc) || $sc[$this->uses[0]]['project_id']!=$pid){
				$this->Session->write('error',_resource('data not found!',true));
				$this->redirectAjaxTo($this->name.'/lists','listContent');
			}
			$data[$this->uses[0]]['project_id']=$pid;
			if($this->{$this->uses[0]}->validates($data)){											
				
				if($this->{$this->uses[0]}->save($data)) {					
					$this->Session->write('message',_resource('insert data successful',true));
					$this->redirectAjaxTo($this->name.'/lists','listContent');					
					$this->data=null;					
					$isInsert=true;

				} else {
					$this->Session->write('error',_resource('can not update data',true));
				}
			}else{
				$this->Session->write('error',_resource('red fill is require',true));
			}
		}else{
			

				
				$data=$this->{$this->uses[0]}->read(null,intval($id));
				if(empty($data)){
					$this->Session->write('error',_resource('data not found!',true));
					$this->redirectAjaxTo($this->name.'/lists','listContent');
				}
				//user join		
				$this->data=$data;						
			
		}

		
		$this->set('isInsert',$isInsert);
		
	}
	
	function admin_changeActive($id=null){
		$id=intval($id);
		$pid=intval($this->Session->read('SourceControl.project_id'));
		//t($pid);
		$projectInfo=$this->getPermission(0,$pid);
		if(empty($projectInfo)){
			$this->Session->write('error',_resource('you have not permission',true));
			$this->redirectAjaxTo('projects','mainContent');
		}
		
		if($id){
			$this->{$this->uses[0]}->updateAll(array('active'=>0),array($this->uses[0].'.id <>'=>$id,$this->uses[0].'.project_id'=>$pid));
			$this->{$this->uses[0]}->updateAll(array('active'=>1),array($this->uses[0].'.id '=>$id,$this->uses[0].'.project_id'=>$pid));
		}
		
		$this->admin_lists();
		

	}

	function admin_delete($id = null) {		
		if(Configure::read('Session.isSa') || Configure::read('Session.isAdmin')){
			$this->Session->delete('error');
			//if is submit:		
			if($this->request->is('post')){
				$id_arr=array_keys($_POST['check']);
			} else {
				$id_arr=null;
			}
			//if url
			if(intval($id)){
				$id_arr[]=$id;
			}
			if(count($id_arr)>0){
				//Xoa nhieu record
				$this->{$this->uses[0]}->deleteAll(array($this->uses[0].'.id'=>$id_arr),false);			
			} else {
				$this->Session->write('error',_resource('no information to delete',true));
			}
		}
		else{
			$this->Session->write('error',_resource('you have not permission',true));
		}
		$this->admin_lists();		
	}
	

	function admin_sort($sortField=null,$sortDir='asc'){
		if($sortField){
			if($sortDir!='desc') $sortDir='asc';
			$this->Session->write($this->uses[0].'.sortField',$sortField);
			$this->Session->write($this->uses[0].'.sortDir',$sortDir);
			$this->Session->write($this->uses[0].'.order',$sortField.' '.$sortDir);
		}
		$this->admin_lists();
	}

	function admin_page($page=1){
		$this->Session->write($this->uses[0].'.page',$page);

		$this->admin_lists();
	}

	function admin_display($limit=0){
		if(intval($limit)){
			$this->Session->write($this->uses[0].'.limit',$limit);
		}
		$this->admin_lists();
	}

	function admin_filter(){
		if($this->request->is('post')){
			$this->Session->write($this->uses[0].'.filterText',$this->request->data['filter_text']);
			$this->Session->write($this->uses[0].'.filterField',$this->request->data['filter_field']);
			//$conditions=$this->Session->read($this->uses[0].'.conditions');
			if(!empty($this->request->data['filter_text'])){				
				$conditions[$this->request->data['filter_field'].' like']='%'.$this->request->data['filter_text'].'%';
				$this->Session->write($this->uses[0].'.page',1);				
			}
			else{
				unset($conditions[$this->request->data['filter_field'].' like']);
			}
			
			$this->Session->write($this->uses[0].'.conditions',$conditions);
		}
		$this->admin_lists();
	}
	
	function admin_status($statusid=0){
		//status
		$statusid=intval($statusid);
		$this->Session->write($this->uses[0].'.status_id',$statusid);		
		$this->Session->write($this->uses[0].'.page',1);
		$this->admin_lists();
	}
	

}
?>
